//
//  ViewController.m
//  Test
//
//  Created by 刘宁银 on 2020/10/23.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableData;

@property (nonatomic, strong) NSMutableArray *toAddData;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //内容不满一屏的时候，为了保证从底部往上滚
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    self.tableData = [[NSMutableArray alloc] init];
    
    self.toAddData = [[NSMutableArray alloc] init];
    for(int i=0;i<1;i++){
        [self.toAddData addObject:@"直播App越来越值得市场关注，那么我们也要越来越关注直播的需求。"];
        [self.toAddData addObject:@"做为一名开发菜鸟，不得不跟随市场的发展而努力的学习。"];
        [self.toAddData addObject:@"打工也要认真，没错就是这句话。"];
    }
}

#pragma mark - UITableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tableData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = self.tableData[indexPath.row];
    return cell;
}

#pragma mark - Actions
- (IBAction)testAction:(id)sender {
//    [self showAddDataWithAnimation];
    
    __weak typeof(self) weakself = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakself addDataWithOneByOneAnimation];
    }];
}

-(void)showAddDataWithAnimation{
    //方式一：一次滚动到底部
    NSMutableArray *array = [[NSMutableArray alloc] init];
    for(NSString *value in self.toAddData){
        [self.tableData addObject:value];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tableData.count-1 inSection:0];
        [array addObject:indexPath];
    }
    [self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView scrollToRowAtIndexPath:array.lastObject atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    

    [UIView animateWithDuration:1 animations:^{
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }];
}

-(void)addDataWithOneByOneAnimation{
    //方式二：一条一条滚动
    [self.tableData addObject:self.toAddData.firstObject];
    [self.toAddData removeObjectAtIndex:0];
    if(self.toAddData.count == 0){
        [self.timer invalidate];
        self.timer = nil;
        [UIView animateWithDuration:1 animations:^{
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        }];
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tableData.count-1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

@end
